package com.nami.dispatcher.application.qos;

import cn.hutool.core.collection.CollUtil;
import com.nami.dispatcher.domain.message.model.PacketTrans;
import com.nami.dispatcher.domain.qos.QosSendDaemonS2C;
import com.nami.dispatcher.facade.qos.ConfirmMsgDeliveryFacade;
import com.nami.dispatcher.facade.qos.request.ConfirmMsgDeliveryCmd;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.core.enums.conver.ConverTypeEnum;
import org.dromara.common.core.utils.SessionUtil;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class ConfirmMsgDeliveryFacadeImpl implements ConfirmMsgDeliveryFacade {


    @Override
    public void execute(ConfirmMsgDeliveryCmd cmd) {
        if (CollUtil.isEmpty(cmd.getMsgIds())) {
            return;
        }
        for (String msgId : cmd.getMsgIds()) {
            if (StringUtils.isBlank(msgId)) {
                continue;
            }
            PacketTrans packetTrans = new PacketTrans();
            packetTrans.setMessageId(msgId);
            packetTrans.setSessionId(SessionUtil.binding(cmd.getSenderId(), cmd.getInstanceId()));
            packetTrans.setIfGroupMsg(ConverTypeEnum.C2C.getCode().equals(cmd.getConverType()) ? false : true);
            QosSendDaemonS2C.getInstance().remove(packetTrans.getFingerprint());
        }

    }

}
